package com.hc.controller;

import com.hc.common.bean.PageBean;
import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.domain.Product;
import com.hc.query.ProductCondition;
import com.hc.res.ProductItem;
import com.hc.service.ProductCategoryService;
import com.hc.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Null;
import java.io.Serializable;
import java.util.List;

/**
 * 产品 controller
 *
 * @author 六月Bing
 */
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @Resource
    private ProductCategoryService productCategoryService;

    @PostMapping
    public Result<Null> create(@RequestBody Product product) {
        boolean res = productService.save(product);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result<Null> updateById(@RequestBody Product product) {
        boolean res = productService.updateById(product);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result<Null> removeById(@PathVariable Serializable id) {
        boolean res = productService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("{id}")
    public Result<Product> getById(@PathVariable Serializable id) {
        Product res = productService.getById(id);
        return ResultUtil.<Product>success("查询成功").setData(res);
    }

    /**
     * 首页大图，显示广告
     *
     * @return
     */
    @GetMapping("index/ads")
    public Result<List<ProductItem>> indexAds() {
        ProductCondition condition = ProductCondition.builder()
                .columns("id,name,price1,price2,img,sell_point")
                .current(1L)
                .pageSize(8)
                //上架
                .status(1)
                .build();
        PageBean<ProductItem> goodsPageBean = productService.queryByCondition(condition);
        List<ProductItem> res = goodsPageBean.getRecords();
        return ResultUtil.<List<ProductItem>>success("查询成功").setData(res);
    }

    /**
     * 4推荐商品 和 5促销商品
     *
     * @return
     */
    //@GetMapping("/index/rp")
    //public Result<List<ProductItem>> getRecommendPromotion() {
    //    ProductCondition condition = ProductCondition.builder()
    //            .current(1L)
    //            .pageSize(6)
    //            .status(4)
    //            .build();
    //    // 推荐商品
    //    List<ProductItem> recommendProductItems = productService.queryByCondition(condition).getRecords();
    //    // 促销商品
    //    condition.setStatus(5);
    //    List<ProductItem> promotionProductItems = productService.queryByCondition(condition).getRecords();
    //    List<ProductItem> res = new ArrayList<>();
    //    res.addAll(recommendProductItems);
    //    res.addAll(promotionProductItems);
    //
    //    return ResultUtil.<List<ProductItem>>success("查询成功").setData(res);
    //}
    //
    ///**
    // * 首页显示商品
    // *
    // * @return
    // */
    //@GetMapping("/index/main")
    //public Result<List<ProductCategoryProductItem>> getByCondition() {
    //    List<ProductCategoryProductItem> productCategoryProductItemList = new ArrayList<>();
    //
    //    //不传递pid表示查询的是顶级父类
    //    ProductCategoryCondition ProductServiceCondition = ProductCategoryCondition.builder()
    //            .columns("id,name")
    //            .current(1L)
    //            .pageSize(3)
    //            .build();
    //    //顶级类别
    //    List<ProductCategoryItem> ProductCategoryItemList = productCategoryService.queryByCondition(ProductServiceCondition).getRecords();
    //    for (ProductCategoryItem productCategoryItem : ProductCategoryItemList) {
    //        ProductCategoryProductItem productCategoryProductItem = new ProductCategoryProductItem();
    //        //设置父类别
    //        productCategoryProductItem.setProductCategoryItem(productCategoryItem);
    //
    //        ProductServiceCondition.setPid(productCategoryItem.getId());
    //        ProductServiceCondition.setPageSize(5);
    //
    //        //二级类别
    //        List<ProductCategoryItem> productCategoryItemList2 = productCategoryService.queryByCondition(ProductServiceCondition).getRecords();
    //        //设置子类别
    //        productCategoryProductItem.setProductCategoryItemList(productCategoryItemList2);
    //
    //        //设置商品
    //        List<Integer> categogryIds = new ArrayList<>();
    //        for (ProductCategoryItem categoryItem : productCategoryItemList2) {
    //            categogryIds.add(categoryItem.getId());
    //        }
    //        if (categogryIds != null && categogryIds.size() > 0) {
    //            ProductCondition productCondition = ProductCondition.builder()
    //                    .columns("id,name,price1,price2,img,sell_point")
    //                    .countryIds(categogryIds)
    //                    .current(1L)
    //                    .pageSize(9)
    //                    .build();
    //            List<ProductItem> productItemList = productService.queryByCondition(productCondition).getRecords();
    //            //设置商品
    //            productCategoryProductItem.setProductItemList(productItemList);
    //        }
    //
    //        productCategoryProductItemList.add(productCategoryProductItem);
    //    }
    //
    //    return ResultUtil.<List<ProductCategoryProductItem>>success("查询成功").setData(productCategoryProductItemList);
    //}
    //

}